/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
/*
		, isRole: function(){
			if(this.infoData["가입여부"] != "0"){
				alert("동호회에 가입되지 않은 회원은 이용하실 수 없습니다.");
				document.location.href = "home.html?club_seq=" + this.infoData["동호회키"];
			}
//			
//			if(!this.objRole.isAdmin(){
//				//alert(this.objRole.getError());
//				document.location.href = "home.html?club_seq=" + this.infoData["동호회키"];
//			}
		}
	}
	*/
	
	
	function clsToll(){
		this._HTMLDom = null;	
		this.listData = null;
		this.idx = null;
		this._init();
	}
	
	clsToll.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(0) td select#select");
			this._HTMLDom = {
				"$layer": $layer	
			}
			var $options = [{
				"코드": "0"
				, "코드명": "회원전체"
			}
			, {
				"코드": "A"
				, "코드명": "일반회원"
			}
			, {
				"코드": "M"
				, "코드명": "매니저"
			}];	
			this.setList($options);	
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			$layer.empty();
			
			for(var i=0;i<this.listData.length;++i){
				$layer.append("<option />");	
			}	
			
			$layer.find("option").each(function(n){				
				$(this).html(thisClass.listData[n]["코드명"]);
			})
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var idx = $layer.find("option").index($layer.find(":checked"));
			
			return this.listData[idx]["코드"];
		}
	}
	
	function clsSendMsg(){
		//$("div#container section#main_content div.club_creat table tbody tr:eq(1) td p").before("<textarea />").addClass("texta").width(720);
		this._HTMLDom = null;
		this.errorMsg = null;
		this._ERRMSG = {
			"_NOVALUE": "내용을 입력하여 주세요."
		}		
		this._init();
	}
	
	clsSendMsg.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(1) td textarea");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.val("");
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);	
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();	
		}
		, isValid: function(){
			if(this.getValue().trim() == "" || this.getValue() == null){
				this.errorMsg = this._ERRMSG["_NOVALUE"];
				return false;	
			}
			return true;
		}
		, getError: function(){
			return this.errorMsg;
		}
	}
	
	function clsComm()
	{
		this.clubOutMemberList = null;
		this.clubOutMemberListPageData = null;
		this.clubOutMemberListRequest = null;		
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){			
			this.clubOutMemberListRequest = {
				"동호회키": null
				, "페이지": null
				, "검색어": null
				, "검색종류": null
				, "정렬키":null
				, "정렬방향":null
			}
			
			this.clubOutMemberListPageData = {
				"전체수": null	
			}
		}	
//------------------------ 탈퇴회원목록조회------------------------//		
		, getClubOutMemberList: function(){			
			var thisClass = this;
		
			var szPage = 1;
			var szSortKey = 0;
			var szSortOption = 0;
			
			//AJAX - 동호회 글목록 조회
			setTimeout(function(){ 
				thisClass.clubOutMemberListPageData = {
					"전체수": 11
				}
				
				thisClass.clubOutMemberList  = [
					{
						"동호회키": "1"
						, "사용자정보": {
							"아이디": "짱말짱"
							, "닉네임": "짱말짱"
							, "이미지": "http://192.168.0.82/images/board/default_50_50.gif"
						}
						, "가입질문답변": "준수"
						, "게시글수": "20120203"
					},
					{
						"동호회키": "1"
						, "사용자정보": {
							"아이디": "오스케"
							, "닉네임": "오스케"
							, "이미지": "http://192.168.0.82/images/board/default_50_50.gif"
						}
						, "가입질문답변": "얼굴로웃길수있음"
						, "게시글수": "20120303"
					},
					{
						"동호회키": "1"
						, "사용자정보": {
							"아이디": "오스케2"
							, "닉네임": "오스케2"
							, "이미지": "http://192.168.0.82/images/board/default_50_50.gif"
						}
						, "가입질문답변": "부끄럽구요~"
						, "게시글수": "20120403"
					}];
				$(thisClass).trigger("evtFinishClubOutMemberList");
			}, 1000);	
		}
		, getClubOutMemberListData: function(){
			return this.clubOutMemberList;
		}
		, getClubOutMemberListPageData: function(){
			return this.clubOutMemberListPageData;	
		}
		, setClubOutMemberListRequest: function(key, val){
			this.clubOutMemberListRequest[key] = val;	
		}
//------------------------ 탈퇴회원목록조회------------------------//		
//------------------------ 쪽지발송------------------------//	
		, InsertSendmail: function(arg){			
			var thisClass = this;
			var szClubSeq = arg["동호회키"];
			var szRole = arg["수신등급"]
			var szMsg = arg["내용"];			
			// ajax 시작			
			//parameter setting and run
			_gf.ajax({
				"sid":"CL001",
				"servicename": "sendClubMessage"
			}, {
				"clubSeq": szClubSeq
				, "targetMemberRole": szRole
				, "messageBody": _util.enter2br(szMsg)
			}, function(ret){				
				
				// callback and mapping				
				var resData = {
					"응답코드": (ret["msp"]["contents"]["serviceResult"] == "00000"?true:false)
					, "메세지": _gf.getResultMsg(ret)
				}				
				$(thisClass).trigger("evtFinishInsertSendMsg", [resData]);
			});		
		}
//------------------------ 쪽지발송------------------------//		
	}
	
	function clsControl()
	{
		this.objComm = null;
		
		this.objClubInfo = null
		this.objRole = null;
		
		this.objToll= null;
		this.objSendMsg = null;
				
		this.club_seq = null;
		this.club_my = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			var member_id = document.objUtil.getCookie("member_id");
			this.club_seq = document.objUtil.getParam("club_seq");
			this.club_my = (document.objUtil.getParam("club_my") == "Y") ? 1:0;
			
			if(!this.club_seq) document.location.href = "../club/club.html";
			
			if(!member_id) {
				if(confirm("로그인 후 이용하여 주세요.")){
					_gf.goLogin();						
				}else{
					document.location.href = "../club/club.html";
				}				
			}else if(!this.club_seq) {
				document.location.href = "../club/club.html";
			}
			
			this._preload();
			
			this.objComm = new clsComm();		
			this.objToll= new clsToll();
			this.objSendMsg = new clsSendMsg();
						
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			$.ajax({
				url: "js/self_club_top.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return (_clubControl.getClubInfo().infoData);
			}, function(){				
				_gf.spinEnd();;
				thisClass._start();
			});	
		}
		, _role: function(){
			if(!this.objRole.isAdmin()){
				alert(this.objRole.getError());
				document.location.href = "home.html?club_seq=" + this.club_seq;
			}
		}
		, _start: function(){		
			this.objClubInfo = _clubControl.getClubInfo();
			this.objRole = new clsRole(this.objClubInfo.infoData);
			this._role();
		}
		, _bind: function(){
			var thisClass = this;
						
			//쪽지보내기 완료이벤트
			$(this.objComm).bind("evtFinishInsertSendMsg", function(event, resData){				
				alert(resData["메세지"]);				
				if(resData["응답코드"]){	
					document.location.href="club_admin_notelist.html?club_seq="+thisClass.club_seq;
				}			
			});
						
			// 가입하기
			$(this.objClubInfo).bind("clubJoin", function(event){
				event.preventDefault();				
				if(thisClass.objClubInfo.infoData["가입유형"] == "Y"){
					document.location.href = "club_join1.html?club_seq="  + thisClass.club_seq;	
				}else if(thisClass.objClubInfo.infoData["가입유형"] == "N"){
					document.location.href = "club_join2.html?club_seq="  + thisClass.club_seq;	
				}
			});
			
			//탈퇴하기
			$(this.objClubInfo).bind("clubClose", function(event){
				event.preventDefault();				
				var submitData = {
					"동호회키" : thisClass.club_seq	
				}				
				thisClass.objComm.clubOutSubmit(submitData);
			});
			
			//동호회탈퇴 완료이벤트
			$(this.objComm).bind("evtFinishClubOut", function(event, resData){	
				alert(resData["메세지"]);								
				if(resData["응답코드"]){
					document.location.href = "home.html?club_seq="  + thisClass.club_seq;
				}	
			});
			
			//쪽지보내기 버튼 이벤트
			$("p.btn_area_box a img[alt=보내기]").click(function(event){
				event.preventDefault();
				
				if(!thisClass.objSendMsg.isValid()){
					alert(thisClass.objSendMsg.getError());
					return;	
				}
				
				var submitData = {
					"동호회키": thisClass.club_seq
					, "수신등급": thisClass.objToll.getValue()
					, "내용": thisClass.objSendMsg.getValue()
				}
				_gf.spinStart();					
				thisClass.objComm.InsertSendmail(submitData);
			});
			
			//보낸쪽지보기  버튼 이벤트
			$("p.btn_area_box a img[alt=발송완료쪽지보기]").click(function(event){
				event.preventDefault();
				document.location.href="club_admin_notelist.html?club_seq="+thisClass.club_seq;
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();	
});	